package com.sptpc.pm2;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.Nullable;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Register extends Activity implements View.OnClickListener {
    EditText etUser,etCodeuser,etPassword,etPasswords;
    Button btuUser;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register);
        etUser=(EditText)findViewById(R.id.et_user);
        etPassword=(EditText)findViewById(R.id.et_password);
        etPasswords=(EditText)findViewById(R.id.et_passwords);
        btuUser=(Button)findViewById(R.id.btu_register);
        btuUser.setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {

        switch (view.getId()){
            case R.id.btu_register:
                String inputEtUser=etUser.getText().toString();
                String inputPassword=etPassword.getText().toString();
                String inputPasswords=etPasswords.getText().toString();
                String regex="^[1]+[3,9]+\\d{9}$";
                Pattern pattern=Pattern.compile(regex);
                Matcher matcher=pattern.matcher(inputEtUser);
                boolean bool=matcher.matches();
//                账号验证
                String pas="((?=.*\\d)(?=.*\\D)|(?=.*[a-zA-Z])(?=.*[^a-zA-Z]))^.{6,16}$";
                Pattern pattern1=Pattern.compile(pas);
                Matcher matcher1=pattern1.matcher(inputPassword);
                boolean passs=matcher1.matches();
                if (inputEtUser.length()!=11 && bool!=true){
                    Toast.makeText(this,"请输入正确的电话号码",Toast.LENGTH_LONG).show();
                }
                else if (!(inputPassword.equals(inputPasswords))){
                    Toast.makeText(this,"两次密码不同",Toast.LENGTH_LONG).show();
                }
                else if (passs!=true){
                    Toast.makeText(this,"请设置包含一个大写，一个小写字母，且长度为6到16位",Toast.LENGTH_LONG).show();
                }

                else {
                    SharedPreferences.Editor editor=getSharedPreferences("data"+inputEtUser,MODE_PRIVATE).edit();
                    Log.d("wang","sp") ;

                    editor.putString("user" ,inputEtUser);
                    Log.d("wang","sp1") ;
                    editor.putString("pass" ,inputPassword);
                    Log.d("wang","sp2") ;
                    editor.apply();
                    Log.d("wang","sp.33") ;
                    Intent intent=new Intent(Register.this,Sign.class);
                    startActivity(intent);
                    Toast.makeText(this,"注册成功",Toast.LENGTH_LONG).show();
                }
        }
    }
}